import { observable, action } from 'mobx';
import { loginApi } from '../fetch/apisetting';
var md5 = require('js-md5');

function getItem(val) {
    return JSON.parse(localStorage.getItem('userData'))?JSON.parse(localStorage.getItem('userData'))[val]:null
}
class AuthStore {
  @observable inProgress = false;
  @observable tips = '';
  @observable logActive = true;
  @observable isRemember = localStorage.getItem('isRemember')=='true'?true:false;
  @observable isAuth = localStorage.getItem('isAuth')=='true'?true:false;
  @observable values = {
    username: localStorage.getItem('userName')||'',
    password: '',
    typeWord:''
  };
  @observable loginparam={
    loginName: '',
    password: '',
    validationCode: '',
    login_type: 1
}
    @observable loginparam={
        loginName: '',
        password: '',
        validationCode: '',
        login_type: 1
    }
  @observable changpwdParam={
        userId:getItem('id'),
        originPwd:'',
        newPwd:'',
        newrePwd:''
    }

  @observable editUserParam={
        "id":getItem('id'),
        "real_name":getItem('real_name'),
         "type":getItem('type'),
          "charge_type":getItem('charge_type'),
          "email":getItem('email'),
      "phone":getItem('phone'),
      "description":getItem('description'),
      "company_id":getItem('company_id'),
      "company":{"name":"","duty_man":"","responsor":"","phone":"","email":"","address":"","licence_code":"l12345","description":"描述信息"}
    }


  @action checkUsername(e){
    let pPattern = /^[a-zA-Z][a-zA-Z0-9~!@#;\$%\^\&\*\(\)_-]/;
    this.values.username=e.target.value
      console.log(this.values.username)
    if(!pPattern.test(e.target.value)){
      this.logActive=true      
      this.tips='用户名以字母开头，字母数字和特殊字符组成！'
    }else{
      this.tips=''
      this.logActive=false
    }
  } 

  @action setPassword(e){
    if(this.tips==''){
      e.target.value.trim()!=''?this.logActive=false:this.logActive=true 
      this.values.password=e.target.value
    }
  } 
  @action checkWord(e){
   
    this.values.typeWord=e.target.value
  } 
  @action toggleCheck(){
    this.isRemember=!this.isRemember
    localStorage.setItem('isRemember',this.isRemember)
  } 

  @action login() {
    this.loginparam.loginName=this.values.username
    this.loginparam.password=md5(this.values.username.trim() + this.values.password.trim())
    this.loginparam.validationCode=window.txt
    return loginApi.getLogin(this.loginparam).then(
      action('getLoginSuccess', res => {
        this.inProgress = false
        localStorage.setItem('isAuth', true)
        localStorage.setItem('userData', JSON.stringify(res.data))
        this.isRemember ? localStorage.setItem('userName', this.values.username) : localStorage.setItem('userName', '')
      }
      ))
  }
  @action getNavData() {
      var sessionData = {
          userName: localStorage.getItem("userName"),
          sessionId: ''
      }
      return loginApi.getNavData(sessionData).then(
          action('getNavData', res => {
              localStorage.setItem('navData', JSON.stringify(res.data))
          })
      )
    }
  @action logout() {
      return loginApi.logout().then((res)=>{
          window.success(res.errormsg)
          this.values.password=''
          localStorage.setItem('isAuth', false)
          localStorage.setItem('navData',null)
      })
  }
    @action changePwd(){
        this.inProgress=true
        return loginApi.changePwd({
            userId:JSON.parse(localStorage.getItem('userData'))?JSON.parse(localStorage.getItem('userData')).id:null,
            originPwd:md5(JSON.parse(localStorage.getItem('userName'))+this.changpwdParam.originPwd),
            newPwd:md5(this.changpwdParam.newPwd),
        }).then(
            action('changePwd',res=>{
                window.success(res.errormsg)
                this.inProgress = false

            })
        )
    }
    @action editUser(){
        this.inProgress=true
        return loginApi.editUser(this.editUserParam).then(
            action('editUser',res=>{
                window.success(res.errormsg)
                this.inProgress = false

            })
        )
    }
}

export default new AuthStore();
